package szoftlab4sunak;

import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultCaret;


public class App extends JFrame implements MouseListener{
	
	public static JPanel contentPane; // az ablak elrendezeset segito panel
	public static JRadioButton tobutt; //  tower letrehozasa
	public static JRadioButton trbutt; // trap letrehozasa
	public static JRadioButton rngbutt; // rangestone letrehozasa
	public static JRadioButton slbutt; // slowstone letrehozasa
	public static JRadioButton dmgbutt; // damagestone letrehozasa
	public static JRadioButton spdbutt; // speedstone letrehozasa
	public static JTextArea textArea; // uzenetek megjeleniteseert felelos objektum
	public static JMenuItem mntmNewgame; // uj jatek kezdese
	public static JMenuItem mntmExit; // kilepes
	public static Init init; // a jatekot kezelo init objektum
	public static Point point; // a kurzor helyet meghatarozo pont
	public static String datas[]; // a megjeleno adatok kiirasat segito attributum
	private static Integer countN = 0; // segedszamlalo
	private static Integer countS = 0; // segedszamlalo
	private static ArrayList<String> ip; // a fajlba irashoz szukseges attributum
	
	//A parancsokat feldolgozo fuggveny
	public static void command(File f, ArrayList<String> op){
		//A jatek inicializalasa
		init = new Init(op);
		//Uj jatek inditasa
		init.newGame();
		//"Parkolo" cella, a meg nem megjelent ellenfelek tarolasara
		init.map.cellStore.put(0, new Cell(0, true,op));
		
		//A parancsok tarolasara szolgalo dinamikus tomb
		ArrayList<String> list = new ArrayList<String>();		
		try {
			//Letrehozzuk a beolvasashoz szukseges objektumokat
			FileReader fr = new FileReader(f);
			BufferedReader br = new BufferedReader(fr);
			while(true){
				//Soronkent olvasunk, a parancsot elmentjuk a tombbe
				String line = br.readLine();
				if(line == null) break;
					list.add(line);
				//Soronkent megyunk vegig a parancsokon, es a "space" karakterek alapjan szettordeljuk oket
				String string = line;
				String[] stringArray = string.split("\\s+");	
				
				if(stringArray[0] != null){
					//Uj cella letrehozasa
					if(stringArray[0].equals("NC")) {
						//Beallitjuk az uj cella ID-jat, majd azt hogy ut-e
						int id = Integer.parseInt(stringArray[1]);
						boolean isroad;
						if(stringArray[2].equals("R"))
							isroad = true;
						else
							isroad = false;
						//Belehelyezzuk a cellakat tarolo tombbe
						Cell temp = new Cell(id, isroad,op);
						init.map.cellStore.put(id, temp);
						if(id!=0){
						init.vmap.vCellStore.put(id, new VCell(temp));}
						}
						
					//Cella szomszedait beallito parancs
					else if(stringArray[0].equals("SN")) {
						//Megjegyezzuk a ket cella ID-jat
						int id1 = Integer.parseInt(stringArray[1]);
						int id2 = Integer.parseInt(stringArray[2]);
						//Meghivjuk a cellak szomszedsagbeallito fuggvenyet, oda-vissza
						init.map.cellStore.get(id1).initNeighbour(init.map.cellStore.get(id2));
						init.map.cellStore.get(id2).initNeighbour(init.map.cellStore.get(id1));
						//Kiiratjuk a kepernyobe es fajlba
						System.out.println("SN - A " + id1 + " szomszedaihoz a " + id2 + " szomszed hozzaadva.");
						op.add("SN - A " + id1 + " szomszedaihoz a " + id2 + " szomszed hozzaadva.");
						}
					//Enemy letrehozo parancs	
					if(stringArray[0].equals("CE")) {
						//Letrehozzuk az enemy-t a megfelelo cellara
						int enemyid = Integer.parseInt(stringArray[1]);
						int cellid = Integer.parseInt(stringArray[2]);
						Cell targetCell = init.map.cellStore.get(cellid);
						//A parancsnak megfelelo enemy letrehozasa, a megfelelo cellara
						if(stringArray[3] != null) {
						// hobbit
						if(stringArray[3].equals("HO")) {
							Hobbit h1 = new Hobbit(enemyid, targetCell, init, op);
							init.map.enemyStore.put(enemyid, h1);
							targetCell.moveTo(enemyid, h1);
							}
						
						//human
						else if(stringArray[3].equals("HU")) {
							Human hu1 = new Human(enemyid, targetCell, init, op);
							init.map.enemyStore.put(enemyid, hu1);
							targetCell.moveTo(enemyid, hu1);
							}
						
						//elf
						else if(stringArray[3].equals("E")) {
							Elf e1 = new Elf(enemyid, targetCell, init, op);
							init.map.enemyStore.put(enemyid, e1);
							targetCell.moveTo(enemyid, e1);
							}
						
						//dwarf
						else if(stringArray[3].equals("D")) {
							Dwarf d1 = new Dwarf(enemyid, targetCell, init, op);
							init.map.enemyStore.put(enemyid, d1);
							targetCell.moveTo(enemyid, d1);
							}
						}
						}
					
					// stone letrehozasa	
					else if(stringArray[0].equals("CS")) {
						//megvizsgaljuk, hogy van-e eleg mana
						if(init.player.getMana() >= 20) {
							int stoneid = Integer.parseInt(stringArray[1]);
							String type = stringArray[2];
							int bdid = Integer.parseInt(stringArray[3]);
							//parancsnak megfelelo kovet belerakjuk a megfelelo BattleDevice-ba
							if(type.equals("range")) {
								RangeStone rangeStone = new RangeStone(bdid);
								BattleDevice bd = init.map.battleDeviceStore.get(bdid);
								bd.putStone(rangeStone, 'r', stoneid);
							}
							else if(type.equals("damage")) {
								DamageStone damageStone = new DamageStone(bdid);
								BattleDevice bd = init.map.battleDeviceStore.get(bdid);
								bd.putStone(damageStone, 'd', stoneid);
							}
							else if(type.equals("slow")) {
								SlowStone slowStone = new SlowStone(bdid);
								BattleDevice bd = init.map.battleDeviceStore.get(bdid);
								bd.putStone(slowStone, 'w', stoneid);
							}
							//csokkentjuk a mana-t
							init.player.decreaseMana(20);
						}
						}
					
					//trap letrehozasa
					if(stringArray[0].equals("CTR")) {
							//van-e eleg mana
							if(init.player.getMana() >= 20) {
							int trapid = Integer.parseInt(stringArray[1]);
							int trcellid = Integer.parseInt(stringArray[2]);
							// a megfelelo cellara letrehozzuk a trape-t
							Cell trtargetCell = init.map.cellStore.get(trcellid);
							Trap trap = new Trap(trapid,op,init,trtargetCell);
							trtargetCell.putBattledevice(trap, 'R', trapid);
							init.map.battleDeviceStore.put(trapid, trap);
							//csokkentjuk a mana-t
							init.player.decreaseMana(20);
						}
					}
					
					//tower letrehozasa
					else if(stringArray[0].equals("CTO")) {
						//van-e eleg mana
						if(init.player.getMana() >= 20) {
							int towerid = Integer.parseInt(stringArray[1]);
							int tocellid = Integer.parseInt(stringArray[2]);
							//torony lerakasa a megfelelo cellara
							Cell totargetCell = init.map.cellStore.get(tocellid);
							Tower tower = new Tower(towerid, totargetCell, init, op);
							totargetCell.putBattledevice(tower, 'O', towerid);
							init.map.battleDeviceStore.put(towerid, tower);
							//mana csokkentese
							init.player.decreaseMana(20);
						}
					}
					
					//Enemy mozgato parancs
					else if(stringArray[0].equals("ME")) {
						int enemyID = Integer.parseInt(stringArray[1]);
						Enemy enemy = init.map.enemyStore.get(enemyID);
						//Enemy direkt leptetese
						enemy.Tick();
						//Kiiratjuk a kepernyobe es fajlba
						System.out.println("ME - Az " + enemyID + " id-u Enemy leptetve.");
						op.add("ME - Az " + enemyID + " id-u Enemy leptetve.");
						}
				
					//Tick parancs kezelese
					else if(stringArray[0].equals("TICK")) {
						//Map tick metodusanak hivasa
						init.map.Tick();
						}
					
					//Enemy eleterejenek lekerdezese
					else if(stringArray[0].equals("GHP")) {
						int eid = Integer.parseInt(stringArray[1]);
						//Megfelelo id-u enemy hp-jat lekerjuk
						int hp = init.map.enemyStore.get(eid).getHP();
						//Kiiratjuk a kepernyobe es fajlba
						System.out.println("GHP - "+ eid +" id-u Enemy hp-ja " + hp);
						op.add("GHP - "+ eid +" id-u Enemy hp-ja " + hp);
						}
					
					//Tower parameter beallitasa
					else if(stringArray[0].equals("STP")) {
						int twid = Integer.parseInt(stringArray[1]);
						String bullettype = stringArray[2];
						boolean fog;
						//kod van-e rajta
						if(stringArray[3].equals("1"))
							fog = true;
						else
							fog = false;
						//beallitja a megadott parancs alapjan a torony parametereket
						init.map.battleDeviceStore.get(twid).setParams(bullettype, fog);
						if(bullettype.equals("dmg") && fog){
							//Kiiratjuk a kepernyobe es fajlba
							System.out.println("STP - Az " + twid + " id-u Tower lovedeke seb es kod van");
							op.add("STP - Az " + twid + " id-u Tower lovedeke seb es kod van");
							}
						else if(bullettype.equals("dmg") && !fog){
							//Kiiratjuk a kepernyobe es fajlba
							System.out.println("STP - Az " + twid + " id-u Tower lovedeke seb es kod nincs");
							op.add("STP - Az " + twid + " id-u Tower lovedeke seb es kod nincs");
							}
						else if(bullettype.equals("cut") && fog){
							//Kiiratjuk a kepernyobe es fajlba
							System.out.println("STP - Az " + twid + " id-u Tower lovedeke vag es kod van");
							op.add("STP - Az " + twid + " id-u Tower lovedeke vag es kod van");
							}
						else if(bullettype.equals("cut") && !fog){
							//Kiiratjuk a kepernyobe es fajlba
							System.out.println("STP - Az " + twid + " id-u Tower lovedeke vag es kod nincs");
							op.add("STP - Az " + twid + " id-u Tower lovedeke vag es kod nincs");
							}
						}
					
					//Enemy parametereinek beallitasa
					else if(stringArray[0].equals("SEP")) {
						//id-a
						int enid = Integer.parseInt(stringArray[1]);
						//hp
						int ehp = Integer.parseInt(stringArray[2]);
						//sebesseg
						int speed = Integer.parseInt(stringArray[3]);
						//lepesig hatra van
						int ttm = Integer.parseInt(stringArray[4]);
						//beallitja a megadott enemyt
						init.map.enemyStore.get(enid).setParams(ehp, speed, ttm);
						//Kiiratjuk a kepernyobe es fajlba
						System.out.println("SEP - " + enid + " id-u ellenfel hp-ja " + ehp + "-ra, speed-je " + speed + "re, ttm-je " + ttm + "-re allitva.");
						op.add("SEP - " + enid + " id-u ellenfel hp-ja " + ehp + "-ra, speed-je " + speed + "re, ttm-je " + ttm + "-re allitva.");
						}
					
					//Lekeri, hogy mennyi mana-ja van a jatekosnak
					else if(stringArray[0].equals("GM")) {
						//Kiiratjuk a kepernyobe es fajlba
						System.out.println("GM - A jatekos mana-ja " + init.player.getMana());
						op.add("GM - A jatekos mana-ja " + init.player.getMana());
						}
					
					//Cella parameter beallitasa
					else if(stringArray[0].equals("SCP")) {
						int cid = Integer.parseInt(stringArray[1]);
						String ctype = stringArray[2];
						//Cel cella
						if(ctype.equals("E"))
							init.map.cellStore.get(cid).setParameters(true, false);
						//Start cella
						else if(ctype.equals("S"))
							init.map.cellStore.get(cid).setParameters(false, true);
						}
					
					//Jatekos manajanak beallitasa megadott ertekre
					else if(stringArray[0].equals("SM")) {
						int mana = Integer.parseInt(stringArray[1]);
						//mana beallitas
						init.player.setMana(mana);
						}
					
					//Adott enemy tulajdonsagainak lekerese
					else if(stringArray[0].equals("GEP")) {
						int genemyid = Integer.parseInt(stringArray[1]);
						//Melyik cellan all
						int pos = init.map.enemyStore.get(genemyid).getPosition();
						//Kiiratjuk a kepernyobe es fajlba
						System.out.println("GEP - Az " + genemyid + " id-u HO tipusu ellenfel Cell id-a " + pos + " .");
						op.add("GEP - Az " + genemyid + " id-u HO tipusu ellenfel Cell id-a " + pos + " .");
						}
						
					}			
				}
			//BufferedReader bezarasa
			br.close();										
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}			
	} 
		
	//A program itt indul
	public static void main(String[] args) {
				
		//frame kezelese
		EventQueue.invokeLater(new Runnable() {
		public void run() {
				try {
					// frame letrehozasa
					App frame = new App();
					frame.setVisible(true);
					// kilepes beallitasa
					mntmExit.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							System.exit(0);
						}
					});
														
				} catch (Exception e) {
					e.printStackTrace();
				}									
			}
		});
		
	}
	
	//app konstruktor
	public App() {
		datas = new String[4];
		//Fajlba irast segito string
		final ArrayList<String> op = new ArrayList<String>();
		//Menusorhoz szukseges elemek letrehozasa
		BufferedReader reader = new BufferedReader(new InputStreamReader (System.in));
		
		// fajl nyitasa , command hivasa a tesztesetre
		File f1 = new File("teszt/6x6initbe.txt");
		command(f1,op);
		try {
			// kimenet fajlba irasa
			PrintWriter pw = new PrintWriter("teszt/teszt.txt");
			for(String s1 : op)
				pw.println(s1);
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		op.clear();	
		
		setTitle("Szoftlab4 grafikus beadas");
		// kilepes beallitasa
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 650, 760);
		contentPane = new JPanel();
		// menu letrehozasa
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		mntmExit = new JMenuItem("Exit");
		menuBar.add(mntmExit);
		// elrendezes bealliatasa		
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);					
	    
	    SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
		// vmap elhelyezese springlayout segitsegevel
		sl_contentPane.putConstraint(SpringLayout.NORTH, init.vmap, 0, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, init.vmap, 0, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, init.vmap, 600, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, init.vmap, -15, SpringLayout.EAST, contentPane);
		contentPane.add(init.vmap);
		// tower-t letrehozo radiobutton elhelyezese springlayout segitsegevel
		tobutt = new JRadioButton("Tower");
		sl_contentPane.putConstraint(SpringLayout.NORTH, tobutt, 6, SpringLayout.SOUTH, init.vmap);
		sl_contentPane.putConstraint(SpringLayout.WEST, tobutt, 0, SpringLayout.WEST, init.vmap);
		contentPane.add(tobutt);
		// trap-et letrehozo radiobutton elhelyezese springlayout segitsegevel
		trbutt = new JRadioButton("Trap");
		sl_contentPane.putConstraint(SpringLayout.NORTH, trbutt, 6, SpringLayout.SOUTH, tobutt);
		sl_contentPane.putConstraint(SpringLayout.WEST, trbutt, 0, SpringLayout.WEST, init.vmap);
		contentPane.add(trbutt);
		// damagestone-t letrehozo radiobutton elhelyezese springlayout segitsegevel
		dmgbutt = new JRadioButton("Damage Stone");
		sl_contentPane.putConstraint(SpringLayout.NORTH, dmgbutt, 6, SpringLayout.SOUTH, trbutt);
		sl_contentPane.putConstraint(SpringLayout.WEST, dmgbutt, 0, SpringLayout.WEST, init.vmap);
		contentPane.add(dmgbutt);
		// slowstone-t letrehozo radiobutton elhelyezese springlayout segitsegevel
		slbutt = new JRadioButton("Slow Stone");
		sl_contentPane.putConstraint(SpringLayout.NORTH, slbutt, 0, SpringLayout.NORTH, dmgbutt);
		sl_contentPane.putConstraint(SpringLayout.WEST, slbutt, 6, SpringLayout.EAST, dmgbutt);
		contentPane.add(slbutt);
		// rangestone-t letrehozo radiobutton elhelyezese springlayout segitsegevel
		rngbutt = new JRadioButton("Range Stone");
		sl_contentPane.putConstraint(SpringLayout.NORTH, rngbutt, 0, SpringLayout.NORTH, trbutt);
		sl_contentPane.putConstraint(SpringLayout.WEST, rngbutt, 0, SpringLayout.WEST, slbutt);
		contentPane.add(rngbutt);
		// speedstone-t letrehozo radiobutton elhelyezese springlayout segitsegevel
		spdbutt = new JRadioButton("Speed Stone");
		sl_contentPane.putConstraint(SpringLayout.NORTH, spdbutt, 6, SpringLayout.SOUTH, init.vmap);
		sl_contentPane.putConstraint(SpringLayout.WEST, spdbutt, 0, SpringLayout.WEST, slbutt);
		contentPane.add(spdbutt);
		// textarea elhelyezese, ez szolgal majd az esemenyek kiiratasara
		textArea = new JTextArea();
		DefaultCaret caret = (DefaultCaret)textArea.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);	
		sl_contentPane.putConstraint(SpringLayout.NORTH, textArea, 15, SpringLayout.SOUTH, init.vmap);
		sl_contentPane.putConstraint(SpringLayout.WEST, textArea, 11, SpringLayout.EAST, rngbutt);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, textArea, -6, SpringLayout.SOUTH, dmgbutt);
		sl_contentPane.putConstraint(SpringLayout.EAST, textArea, 0, SpringLayout.EAST, init.vmap);
		contentPane.add(textArea);
		// radiobutton-ok megfelelo elhelyezese
		ButtonGroup group = new ButtonGroup();	
	    group.add(tobutt);
	    group.add(trbutt);
	    group.add(dmgbutt);
	    group.add(slbutt);
	    group.add(rngbutt);
	    group.add(spdbutt);
		//textArea.addMouseListener(this);    	    
	    init.vmap.addMouseListener(this);
	    
	    //new Thread(init.vmap).start();
	    new Thread(init.map).start();
	}
	
	// az egerkezelo esemeny implementacioja
	@Override
	public void mouseClicked(MouseEvent e) {
		// kattintasi pont lekerese
		point = e.getPoint();
		// torony eseten
		if (tobutt.isSelected()){
			putTower(point);
		}
		// trap eseten
		if (trbutt.isSelected()){
			putTrap(point);
		}
		// slowstone eseten
		if(slbutt.isSelected()){
			putSlowStone(point);
		}
		// rangestone eseten
		if(rngbutt.isSelected()){
			putRangeStone(point);
		}
		// damagestone eseten
		if(dmgbutt.isSelected()){
			putDamageStone(point);
		}
		// speedstone eseten
		if(spdbutt.isSelected()){
			putSpeedStone(point);
		}
	}
	
	// speedstone elhelyezesere szolgalo fuggveny
	private void putSpeedStone(Point p) {
		// ha van a jatekosnak elegendo mana-ja
		if(init.player.getMana() >= 20) {
			for (int i = 1; i <=36; i++){
				// elkerjuk a cella poziciojat
				int x = init.map.cellStore.get(i).getPosX();
				int y = init.map.cellStore.get(i).getPosY();
				// ellenorzes
				if ((p.getX() < x+100) && (p.getY() < y+100)){
					// ha van rajta harci eszkoz, de nincs rajta ko
					if(init.map.cellStore.get(i).isBattleDevice() && !init.map.cellStore.get(i).battledevice.haveStone()){
						countS++;
						// ko letrehozasa majd elhelyezese
						SpeedStone speedStone = new SpeedStone();
						BattleDevice bd = init.map.cellStore.get(i).battledevice;
						bd.putStone(speedStone, 's', countS );
						init.player.decreaseMana(20);
						init.vmap.repaint();
						System.out.println("VAN RAJTA");
					}
					i = 100;
				}
				
			}
		}
	}

	// damagestone elhelyezeset vegzo fuggveny
	private void putDamageStone(Point p) {
		// ha van a jatekosnak elegendo mana-ja
		if(init.player.getMana() >= 20) {
			for (int i = 1; i <=36; i++){
				// elkerjuk a cella poziciojat
				int x = init.map.cellStore.get(i).getPosX();
				int y = init.map.cellStore.get(i).getPosY();
				// ellenorzes
				if ((p.getX() < x+100) && (p.getY() < y+100)){
					// ha van rajta harci eszkoz, de nincs rajta ko
					if(init.map.cellStore.get(i).isBattleDevice() && !init.map.cellStore.get(i).battledevice.haveStone()){
						countS++;
						// ko letrehozasa majd elhelyezese
						DamageStone damageStone = new DamageStone();
						BattleDevice bd = init.map.cellStore.get(i).battledevice;
						bd.putStone(damageStone, 'd', countS );
						init.player.decreaseMana(20);
						init.vmap.repaint();
					}
					i = 100;
				}
				
			}
		}
	}

	// rangestone elhelyezeset vegzo fuggveny
	private void putRangeStone(Point p) {
		// ha van a jatekosnak elegendo mana-ja
		if(init.player.getMana() >= 20) {
			for (int i = 1; i <=36; i++){
				// elkerjuk a cella poziciojat
				int x = init.map.cellStore.get(i).getPosX();
				int y = init.map.cellStore.get(i).getPosY();
				// ellenorzes
				if ((p.getX() < x+100) && (p.getY() < y+100)){
					// ha van rajta harci eszkoz, de nincs rajta ko
					if(init.map.cellStore.get(i).isBattleDevice() && !init.map.cellStore.get(i).battledevice.haveStone()){
						countS++;
						// ko letrehozasa majd elhelyezese
						RangeStone rangeStone = new RangeStone();
						BattleDevice bd = init.map.cellStore.get(i).battledevice;
						bd.putStone(rangeStone, 'r', countS );
						init.player.decreaseMana(20);
						init.vmap.repaint();
					}
					i = 100;
				}
			}
		}
	}

	// slowstone elhelyezeset vegzo fuggveny
	private void putSlowStone(Point p) {
		// ha van a jatekosnak elegendo mana-ja
		if(init.player.getMana() >= 20) {
			for (int i = 1; i <=36; i++){
				// elkerjuk a cella poziciojat
				int x = init.map.cellStore.get(i).getPosX();
				int y = init.map.cellStore.get(i).getPosY();
				// ellenorzes
				if ((p.getX() < x+100) && (p.getY() < y+100)){
					// ha van rajta harci eszkoz, de nincs rajta ko
					if(init.map.cellStore.get(i).isBattleDevice()){
						countS++;
						// ko letrehozasa majd elhelyezese
						SlowStone slowStone = new SlowStone(1);
						BattleDevice bd = init.map.cellStore.get(i).battledevice;
						bd.putStone(slowStone, 'w', countS );
						init.player.decreaseMana(20);
						init.vmap.repaint();
						System.out.println("VAN RAJTA");
					}
					i = 100;
				}
				
			}
		}
	}

	// trap letrehozasat vegzo fuggveny
	private void putTrap(Point p) {
		if(init.player.getMana() >= 20) {
			// ha van a jatekosnak elegendo mana-ja
			for (int i = 1; i <=36; i++){
				// elkerjuk a cella poziciojat
				int x = init.map.cellStore.get(i).getPosX();
				int y = init.map.cellStore.get(i).getPosY();
				// ellenorzes
				if ((p.getX() < x+100) && (p.getY() < y+100)){
					// ha a cella ut
					if (init.map.cellStore.get(i).isRoad()){
						// trap letrehozasa, parametereinek beallitasa majd elhelyezese
						Cell totargetCell = init.map.cellStore.get(i);
						Trap trap = new Trap(countN,ip, init,totargetCell);
						totargetCell.putBattledevice(trap, 'R', countN);
						init.map.battleDeviceStore.put(countN, trap);
						VTrap vTrap = new VTrap();
						vTrap.setOwnTower(trap);
						this.init.vmap.vBattleDeviceStore.put(countN, vTrap);
						countN++;
						init.player.decreaseMana(20);
						init.vmap.repaint();
					}
					i = 100;
				}
			}
		}
		
	}

	// tower letrehozasaert felelos fuggveny
	private void putTower(Point p) {
		// ha van a jatekosnak elegendo mana-ja
		if(init.player.getMana() >= 20) {
			for (int i = 1; i <=36; i++){
				// elkerjuk a cella poziciojat
				int x = init.map.cellStore.get(i).getPosX();
				int y = init.map.cellStore.get(i).getPosY();
				// ellenorzes
				if ((p.getX() < x+100) && (p.getY() < y+100)){
					// ha a cella nem ut
					if (!init.map.cellStore.get(i).isRoad()){
						// tower letrehozasa, parametereinek beallitasa majd elhelyezese
						Cell totargetCell = init.map.cellStore.get(i);
						Tower tower = new Tower(countN, totargetCell, init, ip);
						totargetCell.putBattledevice(tower, 'O', countN);
						init.map.battleDeviceStore.put(countN, tower);
						VTower vTower = new VTower();
						vTower.setOwnTower(tower);
						init.vmap.vBattleDeviceStore.put(countN, vTower);
						countN++;
						init.player.decreaseMana(20);
						init.vmap.repaint();
					}
					i = 100;
				}
			}
		}
	}
	
	// automatikusan generalt fuggveny, nem hasznaljuk
	@Override
	public void mousePressed(MouseEvent e) {
	}
	// automatikusan generalt fuggveny, nem hasznaljuk
	@Override
	public void mouseReleased(MouseEvent e) {
	}
	// automatikusan generalt fuggveny, nem hasznaljuk
	@Override
	public void mouseEntered(MouseEvent e) {
	}
	// automatikusan generalt fuggveny, nem hasznaljuk
	@Override
	public void mouseExited(MouseEvent e) {
	}
		
}
